package controllers;

import java.util.List;

import play.mvc.Controller;
import models.Customer;
import models.Product;
import models.ProductType;

public class Products extends Controller {
	/**
	 * 添加客户
	 * 
	 * @param product
	 */
	public static void addProduct() {
		List<ProductType> productTypeList = ProductType.findAll();		
		render(productTypeList);	
	}
	public static void saveProduct(Product product) {
		System.out.println(product.producttype_id);
		product.save();
		listProduct();		
	}

	/**
	 * 修改客户
	 * 
	 * @param productID
	 */
	public static void editProduct(long productID) {
		Product product = Product.find("id=?", productID).first();
		List<ProductType> productTypeList = ProductType.findAll();
		render(product,productTypeList);
	}

	/**
	 * 删除客户
	 * 
	 * @param productID
	 */
	public static void deleteProduct(long productID) {
		Product product = Product.find("id=?", productID).first();
		product.delete();
		listProduct();	
	}
	
	/**
	 * 客户列表
	 */
	public static void listProduct(){
		List<Product> productList = Product.findAll();
        render(productList);   
	}
	
	public static void search(String searchText){
		List<Product> productsList = Product.find("productName = ?",searchText).fetch();
		render(productsList);
	}

	public static void searchByno(String pno){
		Product product = Product.find("productno = ?",pno).first();
		renderJSON(product);
	}

}
